﻿using PP.Basic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

namespace PP.PPPoker
{
    /// <summary>
    /// NetworkSceneBase 网络场景基类
    /// FromLua: BaseScene.lua
    /// </summary>
    public class NetworkSceneBase : MonoBehaviour
    {
        // Private Variables Begin
        private string Tag => GetType().Name;
        // Private Variables End

        // Lifecycle Methods Begin
        protected void Awake()
        {

        }

        /// <summary>
        /// 从后台切换到前台
        /// </summary>
        protected virtual void OnApplicationEnterForeground()
        {
            Debug.Log($"{Tag}.OnApplicationEnterForeground");
            // 重新连接服务器
        }
        // Lifecycle Methods End
    }

    /// <summary>
    /// 继承自 SceneBase 的单例
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class NetworkSceneBase<T> : NetworkSceneBase where T : Component
    {
        protected static T _instance;
        public static T Instance => _instance;

        // Lifecycle Methods Begin

        protected void Awake()
        {
            // Already exists and not this, then destroy this gameobject and return.
            if (!ReferenceEquals(_instance, null) && !ReferenceEquals(_instance, this))
            {
                Debug.LogWarning($"This should not happen. {typeof(T).Name} should not be duplicated.");
                Destroy(gameObject);
                return;
            }
            _instance = this as T;

            base.Awake();
        }

        protected void OnDestory()
        {
            if (ReferenceEquals(_instance, this))
                _instance = null;
        }
        // Lifecycle Methods End
    }
}